﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows;
using WPFDiagramming.Interfaces;

namespace WPFDiagramming.Designer
{
    public class ConnectorDesigner : IDesigner
    {
        private Point startpoint;
        private Point endpoint;
        private PointOrientation? startpointOrientation;
        private PointOrientation? endpointOrientation;

        public ConnectorDesigner()
        { }

        public ConnectorDesigner(Point startpoint, PointOrientation startpointOrientation, Point endpoint, PointOrientation? endpointOrientation)
        {
            this.startpoint = new Point(startpoint.X, startpoint.Y);
            this.startpointOrientation = startpointOrientation;
            this.endpoint = new Point(endpoint.X, endpoint.Y);
            this.endpointOrientation = endpointOrientation;        
        }

        public Geometry Draw()
        {
            PathGeometry lines = new PathGeometry();

            Point meddle = GetMeddlePoint(startpoint, endpoint);

            List<PathFigure> figures = new List<PathFigure>();
            List<LineSegment> ps = new List<LineSegment>();
            
            if (startpointOrientation == PointOrientation.Left)
            {
                ps.Add(new LineSegment(new Point(startpoint.X - 10, startpoint.Y), true));
                ps.Add(new LineSegment(new Point(startpoint.X - 10, meddle.Y), true));
            }
            else
            {
                ps.Add(new LineSegment(new Point(startpoint.X + 10, startpoint.Y), true));
                ps.Add(new LineSegment(new Point(startpoint.X + 10, meddle.Y), true));
            }

            ps.Add(new LineSegment(new Point(meddle.X, meddle.Y), true));

            figures.Add(new PathFigure(startpoint, ps, false));
            lines.AddGeometry(new PathGeometry(figures));

            List<PathFigure> figures2 = new List<PathFigure>();
            List<LineSegment> ps2 = new List<LineSegment>();

            if (endpointOrientation == null || endpointOrientation == PointOrientation.Left)
            {
                ps2.Add(new LineSegment(new Point(endpoint.X - 10, endpoint.Y), true));
                ps2.Add(new LineSegment(new Point(endpoint.X - 10, meddle.Y), true));
            }
            else
            {
                ps2.Add(new LineSegment(new Point(endpoint.X + 10, endpoint.Y), true));
                ps2.Add(new LineSegment(new Point(endpoint.X + 10, meddle.Y), true));
            }

            ps2.Add(new LineSegment(new Point(meddle.X, meddle.Y), true));
            
            if (endpointOrientation == null)
            {
                this.endpoint.X -= 5;
                this.endpoint.Y -= 5;
            }

            figures2.Add(new PathFigure(endpoint, ps2, false));
            lines.AddGeometry(new PathGeometry(figures2));

            return lines;
        }

        private Point GetMeddlePoint(Point startpoint, Point endpoint)
        {
            var meddleX = Math.Min(startpoint.X, endpoint.X) + Math.Abs((endpoint.X - startpoint.X) / 2);
            var meddleY = Math.Min(startpoint.Y, endpoint.Y) + Math.Abs((endpoint.Y - startpoint.Y) / 2);

            return new Point(meddleX, meddleY);
        }
    }

    public enum PointOrientation
    {
        Left,
        Right
    }
}
